package com.sls.game.service.impl;

import java.io.Serializable;
import java.util.UUID;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.sls.core.socket.Head;
import com.sls.core.socket.ServerDisponseException;
import com.sls.core.util.ServerDateAndTime;
import com.sls.game.assist.Helper;
import com.sls.game.constant.ExceptionCodeDes;
import com.sls.game.dao.DownstreamUserDAO;
import com.sls.game.dao.GoodsOrderDAO;
import com.sls.game.dao.GoodsOrderLogDAO;
import com.sls.game.pojo.DownstreamUserInfo;
import com.sls.game.pojo.GoodsOrderInfo;
import com.sls.game.pojo.GoodsOrderLog;
import com.sls.game.service.BaseService;
@Service("baseServiceImp")
public class BaseServiceImp implements BaseService ,Serializable{
	
	private static final long serialVersionUID = -5508055400054239739L;
	
	private static Logger log = Logger
			.getLogger(BaseServiceImp.class);
	
	// 订单查询dao
	@Resource(name = "goodsOrderDAO")
	private GoodsOrderDAO goodsOrderDAO;

	// 下游用户DAO
	@Resource(name = "downstreamUserDAO")
	private DownstreamUserDAO downstreamUserDAO;
	@Resource(name = "goodsOrderLogDAO")
	private GoodsOrderLogDAO goodsOrderLogDAO;
	/**
	 * 根据订单号，查询订单信息
	 * */
	@Override
	public GoodsOrderInfo queryOrderByOrderId(String orderid,Head head)
			throws ServerDisponseException {

		if (StringUtils.isEmpty(orderid)) {
			throw new ServerDisponseException(
					ExceptionCodeDes.SLS_ORDERID_NULL.getCode(),
					ExceptionCodeDes.SLS_ORDERID_NULL.getDescription());
		}

		GoodsOrderInfo orderInfo = null;
		try {
			orderInfo = this.goodsOrderDAO.queryByOrderId(orderid);
		} catch (Exception e) {
			log.error(
					"queryOrderByOrderId处理[" + head.getRequestId() + "]的请求出现异常！",
					e);
			throw new ServerDisponseException(
					ExceptionCodeDes.ACCESS_DATABASE_ERROR.getCode(),
					ExceptionCodeDes.ACCESS_DATABASE_ERROR.getDescription());
		}

		return orderInfo;
	}

	/**
	 * 根据下游用户号，查询下游用户信息
	 * */
	@Override
	public DownstreamUserInfo queryUserByUserNO(String userNo,Head head)
			throws ServerDisponseException {

		if (StringUtils.isEmpty(userNo)) {
			throw new ServerDisponseException(
					ExceptionCodeDes.SLS_ORDERID_NULL.getCode(),
					ExceptionCodeDes.SLS_ORDERID_NULL.getDescription());
		}

		DownstreamUserInfo downstreamUserInfo = null;
		try {
			downstreamUserInfo = downstreamUserDAO.queryUserByUserNO(userNo);
		} catch (Exception e) {
			log.error(
					"queryUserByUserNO处理[" + head.getRequestId() + "]的请求出现异常！",
					e);
			throw new ServerDisponseException(
					ExceptionCodeDes.ACCESS_DATABASE_ERROR.getCode(),
					ExceptionCodeDes.ACCESS_DATABASE_ERROR.getDescription());
		}
		return downstreamUserInfo;
	}
	
	/**
	 * 更新订单信息
	 * */
	public void updateOrderInfo(GoodsOrderInfo orderInfo,Head head)
			throws ServerDisponseException {
		try{
			goodsOrderDAO.update(orderInfo);
			
		}catch(Exception e){
			log.error(
					"updateGoodsOrder处理[" + head.getRequestId() + "]的请求出现异常！",
					e);
			throw new ServerDisponseException(
					ExceptionCodeDes.ACCESS_DATABASE_ERROR.getCode(),
					ExceptionCodeDes.ACCESS_DATABASE_ERROR.getDescription());
		}
		// 订单日志表插记录
		try {
			GoodsOrderInfo goodsOrderInfo1 = new GoodsOrderInfo();
			goodsOrderInfo1 = this.goodsOrderDAO.queryByOrderId(orderInfo.getOrderId());
			GoodsOrderLog orderLog = Helper.copy(goodsOrderInfo1);
			orderLog.setId(UUID.randomUUID().toString());
			orderLog.setModifyTime(ServerDateAndTime.getNowDateTimeOfFormat());
			this.goodsOrderLogDAO.insert(orderLog);
		} catch (Exception e) {
			log.error("updateGoodsOrder处理[" + head.getRequestId() + "]的请求出现异常！",e);
			throw new ServerDisponseException(
					ExceptionCodeDes.ACCESS_DATABASE_ERROR.getCode(),
					ExceptionCodeDes.ACCESS_DATABASE_ERROR.getDescription());
		}
		
	}

	public GoodsOrderDAO getGoodsOrderDAO() {
		return goodsOrderDAO;
	}

	public void setGoodsOrderDAO(GoodsOrderDAO goodsOrderDAO) {
		this.goodsOrderDAO = goodsOrderDAO;
	}

	public DownstreamUserDAO getDownstreamUserDAO() {
		return downstreamUserDAO;
	}

	public void setDownstreamUserDAO(DownstreamUserDAO downstreamUserDAO) {
		this.downstreamUserDAO = downstreamUserDAO;
	}

}
